package com.example.testlibraryproject.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;

public class LibDeviceInfoUtils {
	private static String tag = "##LibDeviceInfoUtils==";
	/**
	 * Function to return whether used is connected to internet or not
	 * @param context
	 * @return boolean :- true : connected, false: not connected.
	 */
	public static boolean isNetworkConnected(Context context){
		
		ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
	    NetworkInfo ni = cm.getActiveNetworkInfo();
	    return (ni != null && ni.isConnected());
		
	}
	
	/**
	 * Function to return the sdk version of device
	 * @return
	 */
	public static int deviceSdkVersion(){
		return android.os.Build.VERSION.SDK_INT;
	}
	
	/**
	 * Function to return screen resolution of device in pixels.
	 * @param context
	 * @return Point :- point.x = width, point.y = height
	 */
	@SuppressLint("NewApi")
	public static Point getDeviceScreenResolution(Context context){
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		
		Point screenDimension = new Point();
		if(deviceSdkVersion() >=13 ){
			display.getSize(screenDimension);
		}else{
			int width = display.getWidth();  // deprecated
			int height = display.getHeight();	
		}
		  
		return screenDimension;
	}
	
	public static float getDeviceDensity(Context context){
		DisplayMetrics metrics = context.getResources().getDisplayMetrics();
		return metrics.densityDpi;
	}
	
	public static Point getDeviceDensityXY(Context context){
		DisplayMetrics metrics = context.getResources().getDisplayMetrics();
		Point screenDensity = new Point();
		screenDensity.x = (int) metrics.xdpi;
		screenDensity.y = (int) metrics.ydpi;
		
		return screenDensity;
	}
	
	public static String osVersion(){
		return android.os.Build.VERSION.RELEASE;
	}
	
	public static String getDeviceCpuInfo() {
        StringBuffer sb = new StringBuffer();
        sb.append("abi: ").append(Build.CPU_ABI).append("\n");
        if (new File("/proc/cpuinfo").exists()) {
            try {
                BufferedReader br = new BufferedReader(
                    new FileReader(new File("/proc/cpuinfo")));
                String aLine;
                while ((aLine = br.readLine()) != null) {
                    sb.append(aLine + "\n");
                }
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } 
        }
        return sb.toString();
    }
	
	public static String getDeviceTotalRAM() {
	    RandomAccessFile reader = null;
	    String load = null;
	    try {
	        reader = new RandomAccessFile("/proc/meminfo", "r");
	        load = reader.readLine();
	        reader.close();
	    } catch (IOException ex) {
	        ex.printStackTrace();
	    } 
	    return load;
	}
	
	/**
	 * Function to return the height of status bar
	 * @return
	 */
	public static int getStatusBarHeight(Activity act){
		Rect rectgle= new Rect();
		Window window= act.getWindow();
		window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
		int StatusBarHeight= rectgle.top;
		int contentViewTop= 
		    window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
		int TitleBarHeight= contentViewTop - StatusBarHeight;
		
		Log.v(tag, "StatusBarHeight:"+StatusBarHeight+", TitleBarHeight:"+TitleBarHeight);
		return StatusBarHeight;
	}
}
